﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WoWConnecter
{
    public partial class AuthClient
    {
        [PacketMethod(AuthServerOpCode.AUTH_LOGON_CHALLENGE)]
        private void HandleLogonChallenge(AuthPacketIn packet)
        {
            byte unknown = packet.ReadByte();
            byte result = packet.ReadByte();

            AccountStatus status = (AccountStatus)result;

            if (status == AccountStatus.Success)
            {
                //Next
                //byte glength = packet.ReadByte();
                //byte[] g = packet.ReadBytes(glength);
                //byte nlength = packet.ReadByte();
                //byte[] n = packet.ReadBytes(nlength);
                //BigInteger salt = packet.ReadBytes(32);
                //byte[] crcsalt = packet.ReadBytes(16);

                var srpParams = new SecureRemotePassword.SRPParameters();

                BigInteger cred = HashUtilities.HashToBigInteger(SecureRemotePassword.SRPParameters.Hash, string.Format("{0}:{1}", Username, Password));
                SecurePassword = new SecureRemotePassword(Username, cred, false, SecureRemotePassword.SRPParameters.Defaults);
                SecurePassword.PublicEphemeralValueB = new BigInteger(packet.ReadBytes(32));
                BigInteger generator = new BigInteger(packet.ReadBytes(packet.ReadByte()));
                SecurePassword.Modulus = new BigInteger(packet.ReadBytes(packet.ReadByte()));
                SecurePassword.Salt = packet.ReadBytes(32);
                byte[] proof = packet.ReadBytes(16);

                AuthPacketOut packetOut = new AuthPacketOut(AuthServerOpCode.AUTH_LOGON_PROOF);

                packetOut.WriteBigInt(SecurePassword.PublicEphemeralValueA);
                packetOut.WriteBigInt(SecurePassword.ClientSessionKeyProof);
                packetOut.Write(GenerateCrc(proof));
                packetOut.WriteByte(0);
                packetOut.WriteByte(0);
                Send(packetOut);
            }
            else
                if (LoginResult != null)
                    LoginResult(status);
        }

        [PacketMethod(AuthServerOpCode.AUTH_LOGON_PROOF)]
        private void HandleLoginProof(AuthPacketIn packet)
        {
            AccountStatus status = (AccountStatus)packet.ReadByte();

            if (status == AccountStatus.Success)
            {
                AuthPacketOut packetOut = new AuthPacketOut(AuthServerOpCode.REALM_LIST);
                packetOut.Write(0);
                Send(packetOut);
            }
            else
                if (LoginResult != null)
                    LoginResult(status);
        }

        [PacketMethod(AuthServerOpCode.REALM_LIST)]
        private void HandleRealmList(AuthPacketIn packet)
        {
            packet.ReadInt16(); //Length
            packet.ReadInt32(); //Unknown
            short count = packet.ReadInt16();
            Realm[] realms = new Realm[count];

            for (int i = 0; i < count; i++)
            {
                realms[i] = new Realm(packet);
            }

            if (RealmSelection != null)
                RealmSelection(realms);
        }
    }
}
